#import "dfhgwhruebvhrheyluggagerecorder.h"
#import "dfhgwhruebvhrheyprofessionalincidentally.h"
#import "dfhgwhruebvhrheyfacultyauto.h"
#import "dfhgwhruebvhrheylaserbeforehandViewController.h"
#import "dfhgwhruebvhrheyminimalfantasyViewController.h"
@interface dfhgwhruebvhrheyminimalfantasyViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) dispatch_group_t group;
@property (weak, nonatomic) IBOutlet UITableView *tabIV;
@property (nonatomic, strong) dfhgwhruebvhrheyprofessionalincidentally *useModel;
@property (nonatomic, strong) dfhgwhruebvhrheyprofessionalincidentally *sysModel;
@property (nonatomic, strong) NSMutableArray<PPP_CouponItemModel *> *couponList;
@end
static NSString *cellIdentifier = @"dfhgwhruebvhrheyfacultyauto";
@implementation dfhgwhruebvhrheyminimalfantasyViewController
- (void)viewDidLoad {
	[super viewDidLoad];
	[self loadUI];
	[self loadCouponList];
}
- (UIView *)footerView {
	UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, dhgfywegwygeydwdispositionvisualize, 204)];
	footer.backgroundColor = [UIColor clearColor];
	UILabel *tips = [[UILabel alloc] initWithFrame:CGRectMake(13, 20, dhgfywegwygeydwdispositionvisualize-26, 184)];
	NSString *tipsString = @"Notes:\n1. Only one interest-free coupon can be used for each fully amount repaymengt.\n2. Please use the interest-free coupon within vaid date , it will be invaild after expiration.\n3. Please repayment on time , enjoy faster loan approval , higer loan amount,lower loan interesa rate,integrity builds a better life.";
	NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
	style.lineSpacing = 5;
	NSMutableAttributedString *matt = [[NSMutableAttributedString alloc] initWithString:tipsString];
	[matt addAttributes:@{
		NSParagraphStyleAttributeName: style,
		NSFontAttributeName:[UIFont regular:14],
		NSForegroundColorAttributeName: [UIColor whiteColor]
	} range:NSMakeRange(0, tipsString.length)];
	tips.numberOfLines = 0;
	tips.attributedText = matt;
	[footer addSubview:tips];
	return footer;
}
- (void)loadUI {
	[self configBackIcon:@"dhgfywegwygeydwcurvereciprocal"];
	self.navigationStyle = dhgfywegwygeydwreciprocaltransport;
	self.navigationItem.title = @"Interest-free coupon";
	self.couponList = [@[] mutableCopy];
	self.tabIV.tableFooterView = [self footerView];
	self.tabIV.separatorStyle = UITableViewCellSeparatorStyleNone;
	[self.tabIV registerNib:[UINib nibWithNibName:@"dfhgwhruebvhrheyfacultyauto" bundle:nil] forCellReuseIdentifier:cellIdentifier];
}
- (void)bindCouponList {
	[self.couponList removeAllObjects];
	if (self.useModel.code == 200) {
		for (PPP_CouponItemModel *model in self.useModel.data) {
			model.type = 2;
			[self.couponList addObject:model];
		}
	}else {
		if (self.useModel.error.length) {
			[dfhgwhruebvhrheyoptionsaving.keyWindow show:self.useModel.error];
		}
	}
	if (self.sysModel.code == 200) {
		for (PPP_CouponItemModel *model in self.sysModel.data) {
			model.type = 1;
			[self.couponList addObject:model];
		}
	}else {
		if (self.sysModel.error.length) {
			[dfhgwhruebvhrheyoptionsaving.keyWindow show:self.sysModel.error];
		}
	}
	[self.tabIV reloadData];
}
- (void)loadCouponList {
	__weak typeof(self) weakSelf = self;
	if (self.group == nil) {
		self.group = dispatch_group_create();
	}
	dispatch_group_enter(self.group);
	[self systemCoupinList];
	dispatch_group_enter(self.group);
	[self getUserCouponList];
	[dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
	dispatch_group_notify(self.group, dispatch_get_main_queue(), ^{
		__strong typeof(weakSelf) strongSelf = weakSelf;
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
		[strongSelf bindCouponList];
	});
}
- (void)getUserCouponList {
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwlambbreast path:userCouponListApi isEncrypt:YES withParams:nil success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		strongSelf.useModel = [dfhgwhruebvhrheyprofessionalincidentally yy_modelWithJSON:json];
		dispatch_group_leave(strongSelf.group);
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		dispatch_group_leave(strongSelf.group);
	}];
}
- (void)systemCoupinList {
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwlambbreast path:systemCouponListApi isEncrypt:YES withParams:nil success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		strongSelf.sysModel = [dfhgwhruebvhrheyprofessionalincidentally yy_modelWithJSON:json];
		dispatch_group_leave(strongSelf.group);
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		dispatch_group_leave(strongSelf.group);
	}];
}
#pragma mark: ============== UITableViewDelegate ==========
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	__weak typeof(self) weakSelf = self;
	PPP_CouponItemModel *couponModel = self.couponList[indexPath.row];
	if (couponModel.type == 2) {
			if (self.isRepayment) {
				dfhgwhruebvhrheylaserbeforehandViewController *repay = [[dfhgwhruebvhrheylaserbeforehandViewController alloc] init];
				repay.coupon = couponModel;
				repay.fromCouponList = YES;
				[self.navigationController pushViewController:repay animated:YES];
			}else {
				return [dfhgwhruebvhrheyoptionsaving.keyWindow show:@"Please complete your loan application interest-free coupons could be used in repaymenrt!"];
		}
	}else {
		[dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
		NSDictionary *params = @{@"arch_id": couponModel.coupon_id};
		[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwlambbreast path:addCouponApi isEncrypt:NO withParams:params success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
			__strong typeof(weakSelf) strongSelf = weakSelf;
			[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
			dfhgwhruebvhrheysternideology *model = [dfhgwhruebvhrheysternideology yy_modelWithJSON:json];
			if (model.code == 200) {
				couponModel.type = 2;
				[strongSelf.tabIV reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
			}else {
				[dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
			}
		} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
			[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
		}];
	}
}
#pragma mark: ================ UITableViewDataSource ==============
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return self.couponList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	dfhgwhruebvhrheyfacultyauto *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	[cell bindData:self.couponList[indexPath.row]];
	return cell;
}
@end
